#!/usr/bin/python3


from optparse import OptionParser
import sys
import os

sys.path.append("/usr/lib/linx-sec-center")
import ct_core

def create_parser():
    USAGE_MES="%prog [-a|--append]\
            \n  Default: Request no argument, you could input mbc policy in \
            \n          stdin, and when you finished, Ctrl+D will be back and\
            \n          write the new label to system if the The inputs correct\
            \n Example:\
            \n secadmin~$ %prog \
            \n /bin/less\
            \n /tmp/a -rwx\
            \n /home/user1/file r--\
            \n \
            \n /usr/bin/vi\
            \n /opt/file1 -wx\
            \n \
            \n EOF          "
    parse = OptionParser(usage=USAGE_MES,version="%prog 1.0")
    parse.add_option("-a","--append",action="store_true", dest="append", help="add a mbc rules to system")
    parse.add_option("-c","--clear",action="store_true", dest="clear", help="clear up mbc rules of the system")
    return parse

def set_mbc_label(flg=''):
    if flg != '':
        ct_core.get_mbc_label() 

    print("Please input the new mbc policy:")
    lines = sys.stdin.readlines()
    ret = ct_core.get_mbc_label(lines)
    if not ret :
        return 
    ct_core.write_mbc_file()

def clear_mbc_label():
    ct_core.d_mbc_label.clear()
    ct_core.write_mbc_file()
    print("Clear up mbc label done!")

def main():
    parser = create_parser()
    args,flg = parser.parse_args()

    if args.append:
        set_mbc_label('a')
    elif args.clear:
        clear_mbc_label()
    elif not flg:
        set_mbc_label()
    else:
        parser.error("Argument error, Request no argument")

main()
